package treeHoleDAO;

import treeHoleSERVICE.User;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @program:IntelliJ IDEA
 * @Description:AccountInformation的实现类
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/6/19 13:50
 */
public class AccountInformationIMPH implements AccountInformation {
    /**
     * @Description: 创造一个txt文件用于存储我们的账号和密码
     * @Param: []
     * @return:void
     * @Author: Mr.Lin
     * @Date: 2022/6/19 13:57
     */
    @Override
    public void createPublicAccountInformation() {
        //创建我们存储密码的文本文件
        File accountInformationFile = new File("src/main/java/treeHoleDAO/accountInformationFile.txt");
        if (!accountInformationFile.exists()) {
            try {
                accountInformationFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param userPassWord
     * @param userAccount
     * @Description: 将我们账号密码加入我们的txt文件
     * 记得我们每一个密码账号之间需要一个特殊的标志用于分割我们的密码
     * @Param: [user]
     * @return:void
     * @Author: Mr.Lin
     * @Date: 2022/6/19 14:16
     */
    @Override
    public void writeAccountInformation(String userName,int userAccount, String userPassWord) {
        //只有首次注册时候调用
        try {
            File file = new File("src/main/java/treeHoleDAO/accountInformationFile.txt");
            MyObjectOutputStream myObjectInput = new MyObjectOutputStream(new FileOutputStream(file, true));
            User user = new User();
            if (user.userEssayCollection == null)
                user.userEssayCollection = new ArrayList<>();
            user.userEssayCollection = null;
            user.userName = userName;
            user.userAccount = userAccount;
            user.userPassWord = userPassWord;
            user.userEssayCollection = new ArrayList<>();
            myObjectInput.writeObject(user);
            //objectInput.flush();
            myObjectInput.close();
//            else{
//                MyObjectOutputStream myObjectInput = new MyObjectOutputStream(new FileOutputStream(file,true));
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 利用特殊标志符号分割读取我们账号密码
     * 并将其存储我们的集合之中
     * 并将我的读取账号密码存储的集合返回出来
     * @Param: []
     * @return:[]
     * @Author: Mr.yue
     * @Date: 2022/6/16 11:03
     */
    @Override
    public Map<Integer, String> readAccountInformation() {
        Map<Integer, String> readResult = new LinkedHashMap<>();
        //readResult = null;
        MyObjectInputStream objectInput = null;
        try {
            objectInput = new MyObjectInputStream(new FileInputStream("src/main/java/treeHoleDAO/accountInformationFile.txt"));
            Object readObject =null;
            while ((readObject= objectInput.readObject()) != null) {
                User user = new User();
                user.userEssayCollection = new ArrayList<>();
                user = (User) readObject;
                readResult.put(user.userAccount, user.userPassWord);
            }
           // objectInput.close();
        } catch(EOFException e){
//            try {
//                objectInput.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readResult;
    }

    /**
     * @param userAccount
     * @Description: 返回用户名
     * @Param: [userAccount]
     * @return:java.lang.String
     * @Author: Mr.Lin
     * @Date: 2022/6/23 10:27
     */
    @Override
    public String userName(int userAccount) {
        MyObjectInputStream objectInput = null;
        String userName = null;
        User user = new User();
        try {
            objectInput = new MyObjectInputStream(new FileInputStream("src/main/java/treeHoleDAO/accountInformationFile.txt"));
            Object readObject =null;
            while ((readObject= objectInput.readObject()) != null) {
                user.userEssayCollection = new ArrayList<>();
                user = (User) readObject;
                if (userAccount == user.userAccount){
                    userName = user.userName;
                }
            }
            // objectInput.close();
        } catch(EOFException e){
//            try {
//                objectInput.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userName;
    }

    public int useAccount(String userName){
        MyObjectInputStream objectInput = null;
        int userAccount = 0;
        User user = new User();
        try {
            objectInput = new MyObjectInputStream(new FileInputStream("src/main/java/treeHoleDAO/accountInformationFile.txt"));
            Object readObject =null;
            while ((readObject= objectInput.readObject()) != null) {
                user.userEssayCollection = new ArrayList<>();
                user = (User) readObject;
                if (userName.equals(user.userName)){
                    userAccount =user.userAccount;
                }
            }
            // objectInput.close();
        } catch(EOFException e){
//            try {
//                objectInput.close();
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userAccount;
    }
    class MyObjectOutputStream extends ObjectOutputStream{
        public MyObjectOutputStream(OutputStream out) throws IOException {
            super(out);
        }
        protected MyObjectOutputStream() throws IOException, SecurityException {
            super.reset();
    }

        @Override
        protected void writeStreamHeader() throws IOException {
          return;
        }
    }
    class MyObjectInputStream extends ObjectInputStream{


        public MyObjectInputStream(InputStream in) throws IOException {
            super(in);
        }
        @Override
        protected void readStreamHeader() throws IOException, StreamCorruptedException {
        }
    }
}
